﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Common;
using System.Web.Security;

namespace DSAAssignment.Controllers
{
    public class UserController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }
       
        public ActionResult Registration()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Registration(DSAAssignment.Models.Register r)
        {
            User user = new User();
            user.UserName = r.Username;
            user.Password = r.Password;
            user.Email = r.Email;
            user.FirstName = r.Name;
            user.LastName = r.Surname;
            user.HouseNo = r.HouseNo;
            user.StreetName = r.StreetName;
            user.TownName = r.TownName;
            user.CountryID = Convert.ToInt16(r.CountryList.SelectedValue);
            user.MobileNo = r.MobileNo;
            user.Pin = 1234;

            new UserServ.UserServicesClient().AddUser(user);
            return RedirectToAction("HomePage","Home");
        }

        public ActionResult Login()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Login(Models.UserLogin user)
        {
            if(new UserServ.UserServicesClient().UserLogin(user.UserName,user.Password) == true)
                {   
                    FormsAuthentication.SetAuthCookie(user.UserName, user.RememberMe);
                    return RedirectToAction("Index", "Product");
                }
                else
                {
                    ModelState.AddModelError("", "Login data is incorrect!");
                }
            return View("HomePage","Home");
        }

        public JsonResult DoesUsernameExists(string UserName)
        {
            bool doesExist = new UserServ.UserServicesClient().CheckUsernameExists(UserName);

            if (!doesExist)
            {
                return Json(true, JsonRequestBehavior.AllowGet);
            }

            return Json(false, JsonRequestBehavior.AllowGet);
        }

        public JsonResult DoesEmailExists(string Email)
        {

            bool doesExist = new UserServ.UserServicesClient().CheckEmailExists(Email);

            if (!doesExist)
            {
                return Json(true, JsonRequestBehavior.AllowGet);
            }

            return Json(false, JsonRequestBehavior.AllowGet);
        }

        [HttpGet]
        public bool isUserLoggedin()
        {
            if (HttpContext.User.Identity.Name != "")
                return true;
            else
                return false;
        }

        [HttpPost]
        public ActionResult SearchFaultByUserName(string username)
        {
            List<UserProductFault> list = new FaultServ.FaultServiceClient().getAllFaults().Where(x => x.UserName == username).ToList();
            return View("ManageFaults",list);
        
        }


    }
}
